const axios = require('axios');

// 配置
const BASE_URL = 'http://localhost:3000/api';

// 快速获取token
async function quickGetToken() {
  try {
    console.log('🔐 快速获取JWT token...\n');
    
    // 尝试不同的登录方式
    const loginAttempts = [
      {
        name: '手机验证码登录',
        url: '/auth/login',
        data: { phone: '13800138000', code: '123456' }
      },
      {
        name: '邮箱密码登录',
        url: '/auth/login',
        data: { email: 'test@example.com', password: '123456' }
      },
      {
        name: '手机密码登录',
        url: '/auth/login',
        data: { phone: '13800138000', password: '123456' }
      }
    ];
    
    for (const attempt of loginAttempts) {
      try {
        console.log(`📱 尝试: ${attempt.name}`);
        const response = await axios.post(`${BASE_URL}${attempt.url}`, attempt.data);
        
        if (response.data && response.data.data && response.data.data.token) {
          const token = response.data.data.token;
          console.log('✅ 登录成功！');
          console.log(`🔑 JWT Token: ${token}`);
          
          // 显示用户信息
          if (response.data.data.user) {
            const user = response.data.data.user;
            console.log(`👤 用户ID: ${user.id}`);
            console.log(`📱 手机: ${user.phone || '无'}`);
            console.log(`💎 积分: ${user.points || 0}`);
            console.log(`👑 VIP到期: ${user.vipExpiredAt ? new Date(user.vipExpiredAt).toLocaleString() : '无'}`);
            
            // 检查VIP状态
            if (user.vipExpiredAt) {
              const isVip = new Date(user.vipExpiredAt) > new Date();
              console.log(`🎯 VIP状态: ${isVip ? '有效' : '已过期'}`);
            }
          }
          
          // 保存token
          const fs = require('fs');
          fs.writeFileSync('token.txt', token);
          console.log('\n💾 Token已保存到 token.txt');
          
          return token;
        }
      } catch (error) {
        console.log(`❌ ${attempt.name}失败: ${error.response?.status || error.message}`);
        continue;
      }
    }
    
    console.log('\n❌ 所有登录方式都失败了');
    console.log('💡 请检查:');
    console.log('   1. 后端服务是否启动');
    console.log('   2. 登录接口路径是否正确');
    console.log('   3. 用户数据是否存在');
    
    return null;
  } catch (error) {
    console.error('❌ 获取token失败:', error.message);
    return null;
  }
}

// 测试token
async function testToken(token) {
  try {
    console.log('\n🧪 测试token...');
    
    const headers = {
      'Authorization': `Bearer ${token}`,
      'Content-Type': 'application/json'
    };
    
    // 测试短剧详情接口
    const response = await axios.get(`${BASE_URL}/shorts/1`, { headers });
    
    if (response.data) {
      console.log('✅ Token有效！可以访问短剧详情');
      console.log(`📺 短剧标题: ${response.data.title}`);
      console.log(`📚 章节数: ${response.data.chapters?.length || 0}`);
      
      // 检查VIP积分显示
      if (response.data.chapters && response.data.chapters.length > 0) {
        const firstChapter = response.data.chapters[0];
        console.log(`💎 第一章积分: ${firstChapter.points}`);
      }
      
      return true;
    }
  } catch (error) {
    console.error('❌ Token测试失败:', error.response?.data || error.message);
    return false;
  }
}

// 主函数
async function main() {
  const token = await quickGetToken();
  
  if (token) {
    console.log('\n' + '='.repeat(50));
    await testToken(token);
    
    console.log('\n🎉 现在可以使用这个token测试VIP功能了！');
    console.log('\n📝 测试命令:');
    console.log('   node test_vip_points_display.js');
    console.log('   node test_auto_order.js');
  }
}

main().catch(console.error); 